<template>
  <div>
    <BaseTable class="mt-2" :loading="loading" row-key="id" column-code="000108" :data-source="tableData" fill>
      <template #bodyCell="{ column, record, index }">
        <template v-if="column.dataIndex === 'xh'">
          {{ index + 1 }}
        </template>
        <template v-else-if="column.dataIndex === 'advTypeCode'">
          {{ filterCategories(record.advTypeCode) }}
        </template>
        <template v-if="column.dataIndex === 'action'">
          <BaseButton
            confirm-text="确认删除？"
            :disabled="empId !== record.accountEmpId || selectedItem.status !== 0 || selectedItem.chargeStatus !== 0"
            type="text"
            danger
            size="small"
            @click="onDelete(record)"
            >删除</BaseButton
          >
        </template>
      </template>
    </BaseTable>
  </div>
</template>
<script setup>
import { message } from 'ant-design-vue'
import useUserStore from '@/store/user'

const props = defineProps({
  selectedItem: {
    type: Object,
    default: () => ({})
  },
  formState: {
    type: Object,
    default: () => ({})
  }
})

const { empId } = useUserStore()
const { tableData, getData, loading } = useTable({
  url: '/inp/inpComprehensive/getInpExpensesList',
  immediate: false,
  pagination: false,
  formatParams: () => {
    return {
      inplsh: props.selectedItem.inplsh,
      lastDeptCode: props.selectedItem?.lastDeptCode,
      // status: 0,
      accountType: props.formState.accountType
    }
  }
})
const onDelete = async (row) => {
  const { code, msg } = await post('/inp/inpComprehensive/doDelInpExpenses', {
    id: row?.id
  })
  if (code !== 0) return message.error(msg || '删除失败')
  message.success(msg)
  getData()
}
const options = ref([])
const getProjectList = async () => {
  const { code, data } = await post('/drugStorage/comprehenive/getMedicalProjectList_BJZ')
  if (code !== 0) return
  options.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.bh.toString()
    }
  })
}
const filterCategories = (bh) => {
  return options.value.filter((item) => item.value === bh)[0]?.label
}
watch(
  () => props.selectedItem.inplsh,
  (val) => {
    if (val) getData()
  }
)
defineExpose({
  getData
})
onMounted(() => {
  getProjectList()
})
</script>
